package com.appos.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.appos.common.core.controller.BaseController;
import com.appos.common.core.domain.AjaxResult;
import com.appos.domain.AppWorkSheetGroupRelation;
import com.appos.domain.AppWorkSheetRelation;
import com.appos.domain.WorkSheetGroup;
import com.appos.domain.WorkSheetGroupRelation;
import com.appos.service.AppWorkSheetGroupRelationService;
import com.appos.service.WorkSheetGroupRelationService;
import com.appos.service.WorkSheetGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class WorkSheetGroupController extends BaseController {
    @Autowired
    private WorkSheetGroupService workSheetGroupService;

    @Autowired
    private AppWorkSheetGroupRelationService appWorkSheetGroupRelationService;

    @Autowired
    private WorkSheetGroupRelationService workSheetGroupRelationService;

    @PostMapping("/workSheetGroup/save")
    public AjaxResult save(@RequestBody JSONObject postData) {
        WorkSheetGroup workSheetGroup = new WorkSheetGroup();
        String workSheetGroupUuid = (String) postData.get("workSheetGroupUuid");
        workSheetGroup.setWorkSheetGroupUuid(workSheetGroupUuid);
        String workSheetGroupName = (String) postData.get("workSheetGroupName");
        workSheetGroup.setWorkSheetGroupName(workSheetGroupName);
        AppWorkSheetGroupRelation appWorkSheetGroupRelation = new AppWorkSheetGroupRelation();
        String appId = (String) postData.get("appId");
        appWorkSheetGroupRelation.setAppId(appId);
        appWorkSheetGroupRelation.setWorkSheetGroupUuid(workSheetGroupUuid);

        List<Map> workSheetList = (List<Map>) postData.get("workSheetList");
        String workSheetId = (String) workSheetList.get(0).get("workSheetId");

        WorkSheetGroupRelation workSheetGroupRelation = new WorkSheetGroupRelation();

        if(workSheetGroupRelationService.getWorkSheetGroupRelation(workSheetId) == null){
            workSheetList.forEach(o -> {
                workSheetGroupRelation.setWorkSheetId((String) o.get("workSheetId"));
                workSheetGroupRelation.setWorkSheetGroupUuid(workSheetGroupUuid);
                workSheetGroupRelationService.save(workSheetGroupRelation);
            });
        }

        appWorkSheetGroupRelationService.save(appWorkSheetGroupRelation);

        return toAjax(workSheetGroupService.save(workSheetGroup));
    }

    @GetMapping("/workSheetGroup/list")
    public AjaxResult list(@RequestParam String appId){
        List<AppWorkSheetGroupRelation> l = appWorkSheetGroupRelationService.getAppWorkSheetGroupRelation(appId);

        List<String> ids = new ArrayList<>();
        l.forEach(o -> {
            ids.add(o.getWorkSheetGroupUuid());
        });
        return AjaxResult.success(workSheetGroupService.getWorkSheetGroupList(ids));
    }

    @PostMapping("/workSheetGroup/update")
    public AjaxResult update(@RequestBody WorkSheetGroup workSheetGroup) {
        return toAjax(workSheetGroupService.update(workSheetGroup));
    }
}
